<?php 
	namespace app\admin\controller;
	use think\Db;
	class History extends Base{
		public function historyList(){			
			$data = Db::name('history')->order("his_id desc")->paginate(5,false, [ 'type'=>'Bootstrap2' ] );
			$this->assign("num",$data->getCurrentPage()*5-5);
			$this->assign("his_data",$data);
			return $this->fetch();
		}
		public function addHistory(){
			if($_POST){
				$e = 0;//没上传错误
				$data = array(
					'his_time'=>explode('-', $_POST['his_time'])[0].'年',
					'his_incident'=>$_POST['his_incident'],
				);
				$file = $_FILES['file'];
				if($file['name']!=null){
					$type = array('jpg','jpeg','gif','png','pneg');
					$size = pow(1024,2);
						$fileType = pathinfo($file['name'],PATHINFO_EXTENSION);
						if(!in_array($fileType, $type)){
							$e=-1;//图片格式不正确
							
						}
						if($file['size']>$size){
							$e = -2;//上传图片大于2M
							
						}
						if($file['error']!=0){
							$e = -3;//其他错误
							
						}
										
				}else{
					$pic="default.png";
				}
				if($e==0){
					if($file['name']!=null){
						$filename = time().rand(0, 1000).$file['name'];							
						$info = move_uploaded_file($file['tmp_name'], "./upload/admin/history/$filename");						
						$pic=$filename;
					}	
					$data['his_pic'] = $pic;										
					$id = Db::name("history")->insert($data,false,true);
					$this->redirect("historyList");
				}
			}
			return $this->fetch();
		}		
		public function editHistory($id){
			$data = Db::name('history')->where("his_id",$id)->find();
			$this->assign("his",$data);
			return $this->fetch();
		}
		public function saveHistory(){
			if($_POST){
				$e = 0;//没上传错误
				$tid = $_POST['his_id'];
				$data = array(
					'his_time'=>$_POST['his_time'],
					'his_incident'=>$_POST['his_incident'],
				);
				$file = $_FILES['file'];
				if($file['name']!=null){
					$type = array('jpg','jpeg','gif','png','pneg');
					$size = pow(1024,2);
						$fileType = pathinfo($file['name'],PATHINFO_EXTENSION);
						if(!in_array($fileType, $type)){
							$e=-1;//图片格式不正确							
						}
						if($file['size']>$size){
							$e = -2;//上传图片大于2M						
						}
						if($file['error']!=0){
							$e = -3;//其他错误							
						}
						
				}
				if($e==0){
					if($file['name']!=null){
						$filename = time().rand(0, 1000).$file['name'];							
						$info = move_uploaded_file($file['tmp_name'], "./upload/admin/history/$filename");						
						$data['his_pic'] = $filename;	
					}															
					$id = Db::name("history")->where("his_id",$_POST['his_id'])->update($data);
					$this->redirect("historylist");
				}
			}
		}

		public function delHistory($id){
			$data = Db::name('history')->field("his_pic")->where("his_id",$id)->find();
			Db::name("history")->where("his_id",$id)->delete();
			$uri = "./upload/admin/history/".$data['his_pic'];
			if(file_exists($uri)&&$data['his_pic']!='default.png');
			unlink($uri);		
			echo "<script>alert('数据已删除')；</script>";
			$this->redirect("historyList");
		}
	}
	
?>